package lumis.content.source.postloadprocessor;

import org.w3c.dom.Node;

import lumis.content.ContentManagerFactory;
import lumis.content.core.Content;
import lumis.content.core.ContentLocale;
import lumis.doui.source.ISourceData;
import lumis.doui.source.Source;
import lumis.doui.source.TabularData;
import lumis.doui.source.postloadprocessor.IPostLoadProcessor;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.authentication.SessionConfig;
import lumis.util.ITransaction;
import lumis.util.XmlUtil;

/**
 * Post load processor that loads the content name given its identifier.
 *
 * @version $Revision: 11804 $ $Date: 2010-07-29 10:06:21 -0300 (Thu, 29 Jul 2010) $
 * @since 4.2.0
 */
public class ContentNamePostLoadProcessor implements IPostLoadProcessor
{
	@SuppressWarnings("unchecked")
	public void processSource(SessionConfig sessionConfig, Source source, Node parametersNode, ITransaction transaction) throws PortalException
	{
		String contentIdFieldId = XmlUtil.readNodeString("contentIdFieldId", parametersNode);
		if(contentIdFieldId == null)
			throw new UnexpectedException("contentIdFieldId not specified for the ContentNamePostLoadProcessor");

		String contentNameFieldId = XmlUtil.readNodeString("contentNameFieldId", parametersNode);
		if(contentNameFieldId == null)
			contentNameFieldId = contentIdFieldId;
		
		boolean showPublishedContentOnly = showPublishedContentOnly(source);
		
		TabularData tabularData = (TabularData)source.getData();
		for(ISourceData row : tabularData.getRows())
		{
			String contentId = row.get(contentIdFieldId, String.class);
			
			if(contentId != null)
			{
				
				String contentName;
				
				try 
				{
					Content content = ContentManagerFactory.getContentManager().getContent(contentId);
					if(showPublishedContentOnly)
					{
						contentName = content.getContentLocale(sessionConfig.getLocale(), true).getPublishedVersion().getPrimaryName();
					}
					else 
					{
						ContentLocale contentLocale = content.getContentLocale(sessionConfig.getLocale(), true);
						if (contentLocale != null)
						{
							contentName = contentLocale.getActiveVersion().getPrimaryName();
						}
						else
						{
							// ContentLocale not found, so will use null as contentName
							contentName = null;
						}
					}
				} 
				catch (PortalException pe) 
				{
					// Content found, so will use null as contentName
					contentName = null;
				}
				
				row.put(contentNameFieldId, contentName);
			}
		}
	}
	
	/**
	 * Returns if only published content should be shown for the given source.
	 * @param source the source.
	 * @return true if only published content should be shown, otherwise false.
	 * @since 5.6.0
	 */
	protected boolean showPublishedContentOnly(Source<?> source)
	{
		boolean showPublishedContentOnly = true;

		if (source.getDouiContext().getRequest().isPreview())
		{
			showPublishedContentOnly = false;
		}
		else if (source.getDouiContext().getRequest().getServiceInterfaceConfig().isAdministration())
		{
			showPublishedContentOnly = false;
		}

		return showPublishedContentOnly;
	}
	

}
